﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace SalesSys.WebCore
{
   public static  class RequestEx
    {
       public static  NameValueCollection PaserQuery(this HttpRequestBase request)
       {
           //if (request.Url != null)
           //{
           //    var q = request.Url.Query;
           //    var query = HttpUtility.ParseQueryString(q);
           //    return query;
           //}
           return new NameValueCollection(request.QueryString);
       }

       public  static string ToUrlQueryStr(this NameValueCollection nv)
       {
           var urlencoded = true;
           IDictionary excludeKeys = null;
           int count = nv.Count;
           if (count == 0)
           {
               return string.Empty;
           }
           StringBuilder builder = new StringBuilder();
           for (int i = 0; i < count; i++)
           {
               string key = nv.GetKey(i);
               string str3;
               if (urlencoded)
               {
                   key = HttpUtility.UrlEncodeUnicode(key);
               }
               string str2 = !string.IsNullOrEmpty(key) ? (key + "=") : string.Empty;
               var list = nv.GetValues(i);
               int num3 = (list != null) ? list.Length : 0;
               if (builder.Length > 0)
               {
                   builder.Append('&');
               }
               if (num3 == 1)
               {
                   builder.Append(str2);
                   str3 = list[0];
                   if (urlencoded)
                   {
                       str3 = HttpUtility.UrlEncodeUnicode(str3);
                   }
                   builder.Append(str3);
               }
               else if (num3 == 0)
               {
                   builder.Append(str2);
               }
               else
               {
                   for (int j = 0; j < num3; j++)
                   {
                       if (j > 0)
                       {
                           builder.Append('&');
                       }
                       builder.Append(str2);
                       str3 = list[j];
                       if (urlencoded)
                       {
                           str3 = HttpUtility.UrlEncodeUnicode(str3);
                       }
                       builder.Append(str3);
                   }
               }
           }
           return builder.ToString();
       }
    }


}
